<?php
/**
 * 前台基类控制器
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/9/13
 * Time: 11:54
 */
namespace app\common\controller;
use think\Controller;
use think\Session;

class HomeBase extends Controller{
    //初始化函数
    public function _initialize () {
        //判断是否已登录
        if (!Session::get('uid') && !Session::get('username')) {
            $this->redirect('home/login/index');
        }
        //批量分配 公用数据
        $this->assign([
            'group' => db('Group')->select(),
            'user_CM_Data' => db('Userinfo')->where(['uid'=>Session::get('uid')])->find(),
            'group_data' => db('group')->where(['uid'=>Session::get('uid')])->select(),
            'interest' => $this->_interest(),
        ]);
    }

    /**
     * 可能感兴趣的人
     */
    private function _interest() {
        $db = db("follow");
        $uid = session('uid');
        $limit = 4;
        $where = ["fans" => $uid];
        $follow = $db->where($where)->field("follow")->select();
        foreach ($follow as $k => $v){
            $follow[$k] = $v["follow"];
        }
        if (!$follow) {
            $follow[] = 0;
        }
        $ids = implode(",",$follow);
        $sql = "SELECT `id`,`username`,`face50`,`uid`,COUNT(f.`follow`) AS `count`
        FROM `hd_follow` f LEFT JOIN `hd_userinfo` u ON f.follow = u.uid";
        $sql .= " WHERE f.`fans` IN ({$ids}) AND f.`follow` NOT IN ({$ids}) AND f.`follow` != {$uid} GROUP BY f.`follow` ORDER BY `count` LIMIT {$limit}";
        return $db->query($sql);
    }

}
